package main.java.tmi.user.web;

import java.sql.SQLException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.util.AjaxPageSplit;
import main.java.tmi.user.service.UserManageService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


/**
 *  Controller
 *
 * @author min
 * @since 2010. 09. 08.
 */
@Controller
public class UserManageController {
	
    @Resource(name = "userManageServiceImpl")
    private UserManageService userManageService;
    
    /**
     * The Constructor
     */
    public UserManageController() {
        //logger = LogFactory.getLog(this.getClass());
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/user")
    public String viewList(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============user start=========================");
        System.out.println("====================================================");

        return "/tmi/user/userList";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/userDetail")
    public String viewDetail(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============userDetail start=========================");
        System.out.println("====================================================");
        model.addAttribute("userId",param.get("userId"));
        return "/tmi/user/userDetail";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/userWrite")
    public String viewWrite(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============userWrite start=========================");
        System.out.println("====================================================");

        return "/tmi/user/userWrite";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/userUpdate")
    public String viewUpdate(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============userUpdate start=========================");
        System.out.println("====================================================");
        System.out.println("==========================param=========================="+param);
        model.addAttribute("userId",param.get("userId"));
        return "/tmi/user/userUpdate";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/myInfoUpdate")
    public String viewMyInfoUpdate(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============myInfoUpdate start=========================");
        System.out.println("====================================================");
        System.out.println("==========================param=========================="+param);
        model.addAttribute("userId",param.get("userId"));
        return "/tmi/user/myInfoUpdate";
    }
    
    /**
     * search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/getUserManageView")
    public void getUserManageView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getUserManageView start");
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int customeCount = userManageService.getUserManageViewCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(customeCount, pagingTmp, listScale, 5, "");
		System.out.println("===========pageSplit============> "+pageSplit);
		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("customeCount",customeCount);
		model.addAttribute("resultList",userManageService.getUserManageView(param));
    }
    
    /**
     * Detail search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/getUserManageDetailView")
    public void getUserManageDetailView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getUserManageDetailView start");
        
        //조회
		model.addAttribute("resultDetailMap",userManageService.getUserManageDetailView(param));
    }
    
    /**
     * User SAVE
     */
    @RequestMapping("/saveUserManage")
    public void saveUserManage(@RequestParam Map param, Model model) throws Exception {
    	System.out.println("=======================> saveUserManage Start");
    	
    	boolean resultData = userManageService.addUserManage(param);

        //저장 완료 후 조회
        model.addAttribute("result",resultData);
        
    }
    
    /**
     * order de
     */
    @RequestMapping("/delUserManage")
    public void delUserManage(@RequestParam Map param, Model model) throws Exception {
    	System.out.println("=======================> delUserManage Start");
    	
    	boolean resultData = userManageService.delUserManage(param);

        //저장 완료 후 조회
        model.addAttribute("result",resultData);
        
    }
    
    /**
     * User SAVE
     */
    @RequestMapping("/saveMyInfo")
    public void saveMyInfo(@RequestParam Map param, Model model) throws Exception {
    	System.out.println("=======================> saveMyInfo Start");
    	
    	boolean resultData = userManageService.updateMyInfo(param);

        //저장 완료 후 조회
        model.addAttribute("result",resultData);
        
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/getCheckId")
    public void getCheckId(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getCheckId start");		
		model.addAttribute("resultCheckId",userManageService.getCheckId(param));
    }
}
